﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;

namespace CodePortal
{
    public partial class frmServerSetup : TierService.frmTemplate
    {
        public frmServerSetup()
        {
            InitializeComponent();
        }

        private void frmServerSetup_Load(object sender, EventArgs e)
        {
            TierService.IniFile iFile = new IniFile(Application.StartupPath + "\\SocketServer.ini");
            string SktIP = iFile.ReadString("SocketServer", "SktIP", "127.0.0.1");
            string SktPort = iFile.ReadString("SocketServer", "SktPort", "6600");

            txtServerName.Text = SktIP;
            txtPort.Text = SktPort;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            TierService.IniFile iFile = new IniFile(Application.StartupPath + "\\SocketServer.ini");
            iFile.WriteString("SocketServer", "SktIP", txtServerName.Text);
            iFile.WriteString("SocketServer", "SktPort", txtPort.Text);
            MessageBox.Show("保存成功");
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {

            //-----------------Socket 
            string sDbServer = "";
            string sDbDatabase = "";
            string sDbUserID = "";
            string sDBPwd = "";
            string[] DbServerInfo;

            try
            {

                //TierService.IniFile iFile = new IniFile(Application.StartupPath + "\\BrsServer.ini");
                //string SktIP = iFile.ReadString("SocketServer", "SktIP", txtServerName.Text);
                //string SktPort = iFile.ReadString("SocketServer", "SktPort", txtPort.Text);

                TierService.SktClient SocketClint = new SktClient();
                DbServerInfo = SocketClint.RequestDbInfo(txtServerName.Text, txtPort.Text).Split(',');
                sDbServer = DbServerInfo[0];
                sDbDatabase = DbServerInfo[1];
                sDbUserID = DbServerInfo[2];
                sDBPwd = DbServerInfo[3].Replace("\0", "");
            }
            catch (Exception ex)
            {
                MessageBox.Show("连接服务器出错,错误原因为：" + ex.Message);
                return;
            }
            if ((sDbServer == "") || (sDbDatabase == "") || (sDbUserID == "") || (sDBPwd == ""))
            {
                MessageBox.Show("连接服务器出现错误,请重新连接");
                return;
            }
            MessageBox.Show("服务器连接成功");
        }
    }
}